package lee.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * 网关资源类
 */
@Configuration
public class ResourceServerConfig {
    public static final String RESOURCE_ID="res1";


    //1 uaa认证授权服务资源 配置
    @Configuration
    @EnableResourceServer
    public class UAAServerConfig  extends ResourceServerConfigurerAdapter{
        @Autowired
        private TokenStore tokenStore;
        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources
                    //设置我这个resource的id
                    .resourceId(RESOURCE_ID)
                    .tokenStore(tokenStore)
                    //这个貌似是配置要不要把token信息记录在session中
                    .stateless(true);
        }
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/uaa/**").permitAll();//放行所有授权验证请求
        }
    }


    //2 order_server……等等微服务资源
    @Configuration
    @EnableResourceServer
    public class orderServerConfig  extends ResourceServerConfigurerAdapter{
        @Autowired
        private TokenStore tokenStore;
        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources
                    .resourceId(RESOURCE_ID)
                    .tokenStore(tokenStore)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    //本项目所需要的授权范围,这个scope是写在auth服务的配置里的
                    .antMatchers("/order/**").access("#oauth2.hasScope('scope1')");
        }

    }








}
